package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpSystemParameters;
import com.monitor.entity.ErpSystemParametersChild;
import com.monitor.service.ErpSystemParametersChildService;
import com.monitor.service.ErpSystemParametersService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统参数表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-08
 */
@RestController
@RequestMapping("/erpSystemParameters")
@Api(description="系统参数")
public class ErpSystemParametersController {

    @Resource
    private ErpSystemParametersService parametersService;
    @Resource
    private ErpSystemParametersChildService parametersChildService;

    @RequestMapping(value = "/inertOrUpdateParameters",produces = {"application/text;charset=utf-8"})
    @ApiOperation("添加Or修改系统参数")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpSystemParameters> inertOrUpdateCustomerIntegral(ErpSystemParameters parameters) {
        Result<ErpSystemParameters> result=new Result<ErpSystemParameters>();
        try{
            parametersService.insertOrUpdate(parameters);
            result.setCount(1);
            result.setData(parameters);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/removeParameters")
    @ApiOperation("删除系统参数")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "parameterId", value = "系统参数ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeParameters(ErpSystemParameters parameters) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(parametersService.deleteById(parameters)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getparametersById")
    @ApiOperation("根据id查询系统参数")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "parametersId", value = "系统参数ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpSystemParameters> getparametersById(Integer parametersId) {
        Result<ErpSystemParameters> result=new Result<ErpSystemParameters>();
        try{
            ErpSystemParameters parameters1=parametersService.selectOne(new EntityWrapper<ErpSystemParameters>().eq("id",parametersId));
            List<ErpSystemParametersChild> parametersChildList=parametersChildService.selectList(new EntityWrapper<ErpSystemParametersChild>().eq("pId",parametersId));
            parameters1.setParametersChildList(parametersChildList);
            result.setCount(1);
            result.setData(parameters1);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getparametersByName")
    @ApiOperation("根据参数名称查询系统参数")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "parametersName", value = "系统参数名称", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpSystemParameters> getparametersByName(String  name) {
        Result<ErpSystemParameters> result=new Result<ErpSystemParameters>();
        try{
            ErpSystemParameters parameters1=parametersService.selectOne(new EntityWrapper<ErpSystemParameters>().eq("name",name));
            List<ErpSystemParametersChild> parametersChildList=parametersChildService.selectList(new EntityWrapper<ErpSystemParametersChild>().eq("pId",parameters1.getId()));
            parameters1.setParametersChildList(parametersChildList);
            result.setCount(1);
            result.setData(parameters1);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
   @RequestMapping("/getparametersLikeByName")
    @ApiOperation("根据参数名称模糊查询系统参数")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "parametersName", value = "系统参数名称", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
   Result<List<ErpSystemParameters>>  getparametersLikeByName(String  name) {
       Result<List<ErpSystemParameters>>  result=new   Result<List<ErpSystemParameters>> () ;
       List<ErpSystemParameters> parameters1=new ArrayList<>();
        try{
            if (null==name){
                parameters1=parametersService.selectList(new EntityWrapper<ErpSystemParameters>());
            }else{
               parameters1=parametersService.selectList(new EntityWrapper<ErpSystemParameters>().like("name",name));

            }
            for (int i = 0; i < parameters1.size(); i++) {
                ErpSystemParameters erpSystemParameters =  parameters1.get(i);
                List<ErpSystemParametersChild> parametersChildList=parametersChildService.selectList(new EntityWrapper<ErpSystemParametersChild>().eq("pId",erpSystemParameters.getId()));
                erpSystemParameters.setParametersChildList(parametersChildList);
            }

            result.setCount(1);
            result.setData(parameters1);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getParameters")
    @ApiOperation("查询全部系统参数")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "parametersName", value = "系统参数名称", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<List<ErpSystemParameters>> getParameters(@RequestParam(value = "name", defaultValue = "") String  name) {
        Result<List<ErpSystemParameters>> result=new Result<List<ErpSystemParameters>>();
        try{
            List<ErpSystemParameters> parametersList=parametersService.selectList(new EntityWrapper<ErpSystemParameters>());
            for (ErpSystemParameters p:parametersList) {
                List<ErpSystemParametersChild> parametersChildList=parametersChildService.selectList(new EntityWrapper<ErpSystemParametersChild>().eq("pid",p.getId()).like("name",name));
                p.setParametersChildList(parametersChildList);
            }
            result.setCount(parametersList.size());
            result.setData(parametersList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    @RequestMapping("/getByNameParameters")
    @ApiOperation("通过名字查询系统参数")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "parametersName", value = "系统参数名称", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<List<ErpSystemParameters>> getByNameParameters(@RequestParam(value = "name", defaultValue = "") String  name) {
        Result<List<ErpSystemParameters>> result=new Result<List<ErpSystemParameters>>();
        try{

            List<ErpSystemParameters> parametersList=parametersService.selectList(new EntityWrapper<ErpSystemParameters>().eq("name",name));
            for (ErpSystemParameters p:parametersList) {
                List<ErpSystemParametersChild> parametersChildList=parametersChildService.selectList(new EntityWrapper<ErpSystemParametersChild>().eq("pid",p.getId()));
                p.setParametersChildList(parametersChildList);
            }
            result.setCount(parametersList.size());
            result.setData(parametersList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getParametersPage")
    @ApiOperation("查询系统参数分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "parametersName", value = "系统参数名称", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Page<ErpSystemParameters>> getParametersPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                        @RequestParam(value="limit",defaultValue="10") int limit) {
        Result<Page<ErpSystemParameters>> result=new Result<Page<ErpSystemParameters>>();
        try{
            Page<ErpSystemParameters> page1=new Page<ErpSystemParameters>(page,limit);
            Page<ErpSystemParameters> parametersList=parametersService.selectPage(page1,new EntityWrapper<ErpSystemParameters>());
            for (ErpSystemParameters p:parametersList.getRecords()) {
                List<ErpSystemParametersChild> parametersChildList=parametersChildService.selectList(new EntityWrapper<ErpSystemParametersChild>().eq("pId",p.getId()));
                p.setParametersChildList(parametersChildList);
            }
            result.setCount(parametersService.selectCount(new EntityWrapper<ErpSystemParameters>()));
            result.setData(parametersList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }





}

